package com.seecen.controller;

import com.seecen.pojo.User;
import com.seecen.service.UserService;
import com.seecen.until.JwtHelper;
import com.seecen.until.Result;
import com.seecen.until.ResultCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
//跨域问题
@CrossOrigin
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private JwtHelper jwtHelper;

    @RequestMapping("/login")
    public Result login(@RequestBody User user){
        Result result= userService.login(user);
        return result;
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader String token){
        Result result = userService.getUserInfo(token);
        return result;
    }


    @PostMapping("/checkUserName")
    public Result checkUserName(String username){
        Result result = userService.checkUserName(username);
        return result;
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        Result result = userService.register(user);
        return result;
    }
    @GetMapping("/checkLogin")
    public Result checkLogin(@RequestHeader String token){
        boolean expiration = jwtHelper.isExpiration(token);
        if (expiration){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        return Result.ok(null);
    }
}
